﻿$(document).bind("mobileinit", function () {

    $('div[data-role="dialog"]').live("pagecreate", function () {
        if ($(this).attr('data-theme') && $(this).hasClass('ui-body-a')) {
            theme = $(this).attr('data-theme')
            $(this).removeClass('ui-body-a').addClass('ui-body-' + theme)
        }
    })

    $(document).ready(function () {
        $(".no-Ajax").click(function (event) {
            //uncoment to disable the menu's ajax calls
            // $.mobile.ajaxEnabled = false;
        });
    });

    $.setCookie = function (name, value, exdays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + ";path=/";
        document.cookie = name + "=" + c_value;
    }

    $.getCookie = function (name) {
        var i, x, y, ARRcookies = document.cookie.split(";");
        for (i = 0; i < ARRcookies.length; i++) {
            x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
            y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
            x = x.replace(/^\s+|\s+$/g, "");
            if (x == name) {
                return unescape(y);
            }
        }
    }

    $.deleteCookie = function (name) {
        $.setCookie(name, "", -1);
    }

    //    $(document).bind("pagechange", function (evt, args) {
    //        $("[data-role='navbar'] a").removeClass("ui-btn-active")
    //        $("[data-role='navbar'] [data-page-id='" + args.toPage.attr("id") + "'] a").addClass("ui-btn-active")
    //    })


    //$(document).bind("pageload", function () {
    $(document).bind("pagechange", function () {
        //return;

        var rsp = $.getCookie("rsp");
        if (!rsp) {
            $.setCookie("rsp", "", 30);
            rsp = "";
        }

        $.each(rsp.split("|"), function (idx, value) {
            $("[value='" + value + "']").each(function () {
                $(this).attr("checked", true).checkboxradio("refresh");
            });
        });

        //$("input[type='checkbox']").each(function () { alert("checkbox exists!") });
        $("input[type='checkbox']").click(function () {
            //alert("WTF! " + rsp);

            var checkbox = $(this);
            if (checkbox.is(":checked")) {
                //alert("check");
                if (rsp.indexOf(checkbox.val()) == -1) {
                    if (rsp == "")
                        rsp = checkbox.val();
                    else
                        rsp = rsp + "|" + checkbox.val();
                }
            } else {
                //alert("uncheck");
                if (rsp.indexOf(checkbox.val()) >= -1) {
                    var a = rsp.split("|");
                    var index = -1;
                    $.each(a, function (idx, value) { if (value == checkbox.val()) index = idx; });
                    if (index > -1)
                        a.splice(index, 1);
                    rsp = a.join("|");
                }
            }
            $.setCookie("rsp", rsp, 30);

        });
    });
});